# -*- coding: utf-8 -*-
"""
Created on Tue Apr 21 14:47:14 2020

@author: Farman
"""


import numpy as np
import joblib
import sys
import pathlib


def get_predict_dataset(scene):
    '''
    Construct dataset for svm/svc prediction.

    Parameters
    ----------
    scene : numpy array.
        Contains pixel data (3-D).

    Returnss
    -------
    X     : serialized scene data for prediction (1-D).
    shape : origin shape of scene, for prediction preview.
    '''    
    # serialize
    #--------------------------------------------
    (h, w, bands) = scene.shape
    X = np.array([scene[y, x, :] for x in range(w) for y in range(h)])
    return X, scene.shape



def prepare(scene_file):
    '''
    Prepare dataset from data files.
    Sample sample_points points per class.

    Parameters
    ----------
    scene_file : string.
        Name of scene file.

    Returns
    -------
    X     : serialized scene data for prediction (1-D).
    shape : origin shape of scene, for prediction preview.
    '''
    scenes = joblib.load(scene_file)
    scene  = scenes.get(list(scenes.keys())[0])
    
    X, shape = get_predict_dataset(scene)
    return X, shape


def prepare_data_file(scene_file):
    '''
    Prepare dataset for prediction from scene file.

    Parameters
    ----------
    scene_file : string.
        Name of scene file.
        
    Returns
    -------
    Persistence data file contains:
        X with name 'Xy.joblib'
        shape with name 'shape_pred.joblib'
        

    '''
    path = pathlib.Path(scene_file).parent
    X, shape = prepare(scene_file)
    joblib.dump(X, path / 'X_pred.joblib')
    joblib.dump(shape,  path / 'shape_pred.joblib')
    return

'''
scene_file = r'D:\Pavia\Pavia\PaviaUniversity\PaviaU.joblib'
truth_file = r'D:\Pavia\Pavia\PaviaUniversity\PaviaU_gt.joblib'
X, y, shape = prepare(scene_file, truth_file)
'''

if __name__ == '__main__':
    if len(sys.argv) == 1:
        scene_file = input('Scene file : ')
        
        if len(scene_file):
            prepare_data_file(scene_file)
            print('Job done.')
        else:
            print('Usage:')
            print('    python3 svc2dataset4predict.py scene_file')
        
        
    elif len(sys.argv) == 2:
        scene_file = sys.argv[1]
        prepare_data_file(scene_file)
        
    else:
        print('Usage:')
        print('    python3 svc0dataset.py')
        print('or')
        print('    python3 svc0dataset.py scene_file truth_file')